package com.liuyc.lib.ui.xrecycleview

import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.liuyc.lib.ui.R

class CustomLoadingMoreFooter @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private var mText: TextView? = null
    private var mAnimationDrawable: AnimationDrawable? = null
    private var mIvProgress: ImageView? = null
    private var customMoreInterface: CustomMoreInterface? =null
    init {
        initView(context)

    }

    companion object {

        const val STATE_LOADING = 0
        const val STATE_COMPLETE = 1
        const val STATE_NOMORE = 2
    }

    fun initView(context: Context?) {
        LayoutInflater.from(context).inflate(R.layout.refresh_footer, this)
        mText = findViewById<View>(R.id.msg) as TextView
        mIvProgress = findViewById<View>(R.id.iv_progress) as ImageView
        mAnimationDrawable = mIvProgress!!.drawable as AnimationDrawable
        if (!mAnimationDrawable!!.isRunning) {
            mAnimationDrawable!!.start()
        }
        layoutParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    }

    fun setState(state: Int) {
        when (state) {
            STATE_LOADING -> {
                if (!mAnimationDrawable!!.isRunning) {
                    mAnimationDrawable!!.start()
                }
                mIvProgress!!.visibility = GONE
                mText!!.text = context.getText(R.string.data_loading)
                this.visibility = VISIBLE
            }
            STATE_COMPLETE -> {
                if (mAnimationDrawable!!.isRunning) {
                    mAnimationDrawable!!.stop()
                }
                mText!!.text = context.getText(R.string.data_loading)
                this.visibility = GONE
            }
            STATE_NOMORE -> {
                if (mAnimationDrawable!!.isRunning) {
                    mAnimationDrawable!!.stop()
                }
                mText!!.text = context.getText(R.string.nomore_loading)
                mText!!.setTextColor(context.resources.getColor(R.color.Blue))
                mText!!.setOnClickListener {
                    customMoreInterface?.onClickMore()
                }
                mIvProgress!!.visibility = GONE
                this.visibility = VISIBLE
            }
        }
    }

    fun reSet() {
        this.visibility = GONE
    }
    interface CustomMoreInterface{
        fun onClickMore()
    }
    fun setMoreInterfaceListener ( customMoreInterface: CustomMoreInterface){
        this.customMoreInterface=customMoreInterface
    }
}